<?php


class EditFeature{
	
	public $id=0;
	public $nome="";
	public $detalhe="";
	public $user_id=0;
	public $ordem=0;
	public $classificacao=0;
	public $tipo=0;	
	
	public function __construct(){

		
		switch ($_SERVER['REQUEST_METHOD']) {
			case 'GET':
				
				switch( $_GET['action']){
					case 'edit':
						
						$feature_id		=	$_GET['feature_id'];
						$result			=	$this->getFeature( $feature_id );
						
						$this->id		=	$feature_id;
						$this->nome		=	$result->nome;						
						$this->detalhe	=	$result->detalhe;	
						$this->user_id	=	$result->user_id;	
						$this->ordem	=	$result->ordem;	
						$this->classificacao	=	$result->classificacao;	
						$this->tipo	=	$result->tipo;	
						
						
					case 'del':
						
						
						break;

						
					default:
						
						global $app;

						$this->rowslist		=	$this->getRows( $app->getUser_id() );
						
						
						break;
				}
				
				
				break;
			case 'POST':
										
				
				switch( $_POST['action']){
					case 'edit':
						
						global $app;
						
						$_POST['user_id']=	$app->getUser_id();
						$result			=	$this->setFeature( (object) $_POST );						
					
						if(empty($_POST['feature_id'])){
							
						}else{
							$this->id	=	$_POST['feature_id'];
						}
						
						
						$this->nome		=	$_POST['nome'];						
						$this->detalhe	=	$_POST['detalhe'];	
						$this->user_id	=	$_POST['user_id'];	
						$this->ordem	=	$_POST['ordem'];	
						$this->classificacao	=	$_POST['classificacao'];	
						$this->tipo	=	$_POST['tipo'];	
						
						
						header("Location: gridFeature.php", TRUE, 301); 
						
						
						break;
						
					case 'del':
						
						global $app;
						
						$user_id		=	$app->getUser_id();
						$result			=	$this->removeFeature( $_POST['feature_id'], $user_id );
						
						header("Location: index.php", TRUE, 301); 
						
						break;
						
					default:
						
						break;
				}
				
				
				break;
			default:
				exit("Caia fora");
				break;
		}
		

		
	}

	
	function splitWhere( $where )
	{
		
		return ( count( $where ) ? 
								' WHERE ' . implode( ' AND ', $where ) : 
								'' );
	}
		
	
	public function getGrid( $rows ){
		
		$result = "";
		
		$result .="<tr>";
							
		$result .=	"<td>Id</td>";
		$result .=	"<td>Nome</td>";
		$result .=	"<td>Detalhe</td>";
		$result .=	"<td>Ordem</td>";
		$result .=	"<td>classificacao</td>";
		$result .=	"<td>tipo</td>";
		
		$result .="</tr>";
			
		
		foreach( $rows as $key=>$item){
			
			$result .="<tr>";
							
			$result .=	"<td><a href='editFeature.php?action=edit&feature_id=".$item->id."'><img src='images/icon/edit.png' border='0'></a></td>";
			$result .=	"<td>".$item->nome."</td>";
			$result .=	"<td>".$item->detalhe."</td>";
			$result .=	"<td>".$item->ordem."</td>";
			$result .=	"<td>".$item->classificacao."</td>";
			$result .=	"<td>".$item->tipo."</td>";
			
			$result .="</tr>";
		}
		
		
		
		return "<table border=1 width='100%'>".$result."</table>";
	}	
	
	
			
	function getRows( $user_id=0, $limit_start=0, $limit_end=10 )
	{

		$result	=	"";
		$config	=	new Configuration();	
		
		$db 	=	new MySQL( $config->db_server, 
							$config->db_username, 
							$config->db_password, 
							$config->db_name );

		$where	=	array();

		$where[]=	'user_id = \''.$user_id.'\'';
		
		$where	=	$this->splitWhere( $where );
				
																
		$query 	=	'SELECT '
					. ' '				
					. ' * '
					. ' '
					. ' FROM '.$config->db_table_semantic_web_feature.' '
					. $where;

		$rows	=	$db->getQuery($query);
		
		if(count( $rows )>0 )
			$result=	$rows;

		
		return $result;
	}
	
	
			
	function getFeature( $feature_id=0 )
	{

		$result	=	"";
		$config	=	new Configuration();	
		
		$db 	=	new MySQL( $config->db_server, 
							$config->db_username, 
							$config->db_password, 
							$config->db_name );

		$where	=	array();

		$where[]=	'id = \''.$feature_id.'\'';
		
		$where	=	$this->splitWhere( $where );
				
																
		$query 	=	'SELECT '
					. ' '				
					. ' * '
					. ' '
					. ' FROM '.$config->db_table_semantic_web_feature.' '
					. $where;

		$rows	=	$db->getQuery($query);
		
		if(count( $rows )>0 )
			$result=	$rows[0];
		
		return $result;
	}
	
	
	
	function removeFeature( $feature_id, $user_id )
	{

		$result	=	"";
		$config	=	new Configuration();	
		
		$db 	=	new MySQL( $config->db_server, 
							$config->db_username, 
							$config->db_password, 
							$config->db_name );
												
		
		$where	=	array();
			
		$where[]=	'id = '.$feature_id.'';
		$where[]=	'user_id = '.$user_id.'';
		
		$where	=	$this->splitWhere( $where );
				
		$result	=	$this->getFeature( $args->application_id );
		
		
		if( empty($result) ){
			//insert
			
			$query 	=	'DELETE FROM '
						. '  '.$config->db_table_semantic_web_feature.' '
						.$where;
						
		}


		$rows	=	$db->setQuery($query);
		
		if(count( $rows )>0 )
			$result=	$rows[0];
		
		return $result;
	}


	function setFeature( $args )
	{

		$result	=	"";
		$config	=	new Configuration();	
		
		$db 	=	new MySQL( $config->db_server, 
							$config->db_username, 
							$config->db_password, 
							$config->db_name );
												
		
		$where	=	array();
			
		$where[]=	'id = '.$args->feature_id.'';
		
		$where	=	$this->splitWhere( $where );
		
		
		$result			=	$this->getFeature( $args->feature_id );
		
		if( empty($result ) ){
			//insert
			
			$query 	=	'INSERT INTO '
						. '  '.$config->db_table_semantic_web_feature.' '
						.' (nome,detalhe,user_id,ordem,classificacao,tipo) '
						.' values '
						.' ("'.addslashes($args->nome).'",'
						.' "'.addslashes($args->detalhe).'",'
						.' '.addslashes($args->user_id).','
						.' '.addslashes((int)$args->ordem).','
						.' '.addslashes((int)$args->classificacao).','
						.' '.addslashes((int)$args->tipo).' )';
						
						
		}else{
			//update
																	
			$query 	=	'UPDATE '
						. '  '.$config->db_table_semantic_web_feature.' '
						.' SET '
						.' nome="'.addslashes($args->nome).'",'
						.' detalhe="'.addslashes($args->detalhe).'",'
						.' user_id='.addslashes($args->user_id).', '
						.' ordem='.addslashes((int)$args->ordem).', '	
						.' classificacao='.addslashes((int)$args->classificacao).', '	
						.' tipo='.addslashes((int)$args->tipo).' '						
						. $where;
			
		}
		
		
		//exit($query);
		$rows	=	$db->setQuery($query);
		
		
		if( empty($result ) )
			$this->id	=	$db->getLastInsertId();			
									
		
		
		if(count( $rows )>0 )
			$result=	$rows[0];
		
		return $result;
	}
	
	
}

?>